package com.dhecp.project.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.project.system.domain.SysDictionaryRelationForField;
import com.dhecp.project.system.service.SysDictionaryRelationForFieldService;

/**
 * 调入字段信息 控制层
 * @author lwl
 * @version 2019年11月27日
 */
@Controller
@RequestMapping("/system/dictionaryrelationforfield")
public class SysDictionaryRelationForFieldController extends BaseController{

//	private String prefix = "system/dictionaryrelationforfield";
	private String moduleTable = ModuleTable.DICTIONARY_RELATION_FOR_FIELD;
	
	@Autowired
	private SysDictionaryRelationForFieldService dictionaryRelationForFieldService;
	
	
	@RequiresPermissions("system:dictionaryrelationforfield:view")
    @GetMapping("/field")
    public ModelAndView dataEditList(@RequestParam Map<String, Object> params)
    {
    	ModelAndView mv = this.getModelAndView();
        mv.setViewName("system/dictionaryrelationforfield/relation");
		return mv;
    }
	
	/**
     * 查询所有关联调入字段信息
     * @param params
     * @return TableDataInfo
     * @author lwl
     */
    @RequiresPermissions("system:dictionaryrelationforfield:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
    	
    	params.put("moduleTable", moduleTable);
        List<SysDictionaryRelationForField> list = new ArrayList<SysDictionaryRelationForField>();
        try {
            list = dictionaryRelationForFieldService.queryDictionaryRelationForField(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }
    
    /**
     * 新增调入字段——数据提交
     * @param dictionaryRelationForFieldItems 必要参数：对应实体类的json格式字符串
     * @return AjaxResult
     * @author lwl
     */
    @RequiresPermissions("system:dictionaryrelationforfield:add")
    @Log(title = "新增调入字段", businessType = BusinessType.INSERT)
    @PostMapping("/addpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (params.get("dictionaryRelationForFieldItems") == null || params.get("dictionaryRelationForFieldItems") == ""){
        	return AjaxResult.error("缺少参数dictionaryRelationForFieldItems【对应实体类的json格式字符串】。");
        }
        try {
            ajaxResult= dictionaryRelationForFieldService.insertDictionaryRelationForField(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 删除调入字段信息
     * @param dictionaryRelationForFieldItems
     * @return AjaxResult
     */
    @RequiresPermissions("system:dictionaryrelationforfield:delete")
    @Log(title = "删除调入字段", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult delete(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= dictionaryRelationForFieldService.deleteDictionaryRelationForField(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 修改调入字段信息--提交数据
     * @param dictionaryRelationForFieldItems
     * @return AjaxResult
     */
    @RequiresPermissions("system:dictionaryrelationforfield:edit")
    @Log(title = "修改调入字段信息", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (params.get("dictionaryRelationForFieldItems") == null || params.get("dictionaryRelationForFieldItems") == ""){
        	return AjaxResult.error("缺少参数dictionaryRelationForFieldItems【对应实体类的json格式字符串】。");
        }
        try {
            ajaxResult= dictionaryRelationForFieldService.updateDictionaryRelationForField(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}
